﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;

namespace weba
{
    /// <summary>
    /// wajax 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [GenerateScriptType(typeof(dituwz))]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    [System.Web.Script.Services.ScriptService]
    public class wajax : System.Web.Services.WebService
    {
        sjklqDataContext sjk = new sjklqDataContext();
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        public string xiougai(string x, string y, string id)
        {
            sjk.xg(x, y, id);
            return "更新成功";
        }
        [WebMethod]
        public string genxing(string x, string y, string id)
        {
            dituwz dz = sjk.dituwz.Single(h => h.Id.ToString() == id);
            dz.lx = x;
            dz.ly = y;
            sjk.SubmitChanges();
            return "更新成功";
        }
        [WebMethod]
        public List<dituwz> getbyid(string pid)
        {
            List<dituwz> ld = new List<dituwz>();
            List<dituwz> lds = sjk.dituwz.Where(h => h.Id.ToString() == pid).ToList();
            ld.AddRange(lds);
            foreach (dituwz z in lds)
            {
                GetChild(ld,int.Parse( z.Id.ToString()));
            }
            return ld;
        }

        private void GetChild(List<dituwz> ld, int p)
        {
            if (sjk.dituwz.Where(h => h.pid.ToString() == p.ToString()).Count() == 0)
            {
                return;
            }
            else {
                List<dituwz> d = sjk.dituwz.Where(h => h.pid.ToString() == p.ToString()).ToList();
         ld.AddRange(d);
          //  throw new NotImplementedException();
         foreach (dituwz z in d)
         {
             GetChild(ld, int.Parse(z.Id.ToString()));
         }
            }
        }
    }
}
